﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.Common;
using Dapper;
using System.Linq;

namespace AgencyManagerSystem.DataFetcher
{
    public class DomainFetcher : BaseFetcher<DomainModel>
    {
        public DomainFetcher() : base()
        {

        }
        public DomainModel GetByCode(string code)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Code", Op = "=", Value = code } };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            else
                return null;
        }
        public IList<DomainModel> ListByLoginCode(string userCode)
        {
            string sqlstr = "SELECT * FROM SYS_DOMAIN WHERE AUTOID IN (SELECT DOMAIN FROM SYS_ROLEUSER WHERE USER IN (SELECT AUTOID FROM SYS_USER WHERE CODE = @CODE))";
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("@CODE", userCode);
            LogHelper.Debug(this.GetType().Name, sqlstr);
            IList<DomainModel> result = new List<DomainModel>();
            try
            {
                result = DBHelper.GetTran().Query<DomainModel>(sqlstr, dynamicParameters).AsList();
            }
            catch (Exception ex)
            {
                LogHelper.Err(this.GetType().Name, ex.Message + "-" + ex.StackTrace);
            }
            return result;
        }
    }
}
